//#include "Optimizater.h"

void Optimizater::ScanOptimal(int geomsize,int batchs,int cntpass = 5)
{
	debug::Log<<"start scan optimization"<<std::endl;
	debug::Log<<"geomsize:"<<geomsize<<" batchs:"<<batchs<<std::endl;

	__int64 mintime = 1000000*geomsize*batchs;

//	Surface *ts;
	int cv = geomsize*2;
	int ci = geomsize*3;

	Vertex *v = new Vertex[cv];
	unsigned short *sind = new unsigned short[ci];
	unsigned int *ind = new unsigned int[ci];

	for(int i=0;i<cv;++i)
	{
		v[i].x = rnd(-0.46f,0.46f);
		v[i].y = rnd(-0.46f,0.46f);
		v[i].z = rnd(-0.46f,0.46f);

		Vector3D norm;
		norm.getRandomVector();

		v[i].nx = norm.x;
		v[i].ny = norm.y;
		v[i].nz = norm.z;

		v[i].u0 = rnd(0.0f,1.0f);
		v[i].v0 = rnd(0.0f,1.0f);

		v[i].r = rnd(0.0f,1.0f);
		v[i].g = rnd(0.0f,1.0f);
		v[i].b = rnd(0.0f,1.0f);
		v[i].a = rnd(0.0f,1.0f);
	}

	for(int i=0;i<ci;++i)
	{
		sind[i]=i*2 / 3;
		ind[i]=sind[i];
	}

	unsigned int indices16,indices32,vertexB;

	glGenBuffers(1,&indices16);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB,indices16); 
	glBufferData( GL_ELEMENT_ARRAY_BUFFER_ARB,sizeof(unsigned short int)*ci,sind,GL_STATIC_DRAW_ARB);

	glGenBuffers(1,&indices32);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB,indices32); 
	glBufferData( GL_ELEMENT_ARRAY_BUFFER_ARB,sizeof(unsigned int)*ci,ind,GL_STATIC_DRAW_ARB);

	glGenBuffers(1,&vertexB);		
	glBindBuffer(GL_ARRAY_BUFFER_ARB,vertexB);
	glBufferData( GL_ARRAY_BUFFER_ARB,cv*sizeof(Vertex),v, GL_STATIC_DRAW_ARB );

	glDisable(GL_DEPTH_TEST);
	glTranslatef(0,0,-5);
	//glColorMask(0,0,0,0);
	__int64 time = 0;
	__int64 i16s=0,i32s=0;
	for(int fc = 0; fc<cntpass;++fc)
	{
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
		//pass 0
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB,indices16);
		glBindBuffer(GL_ARRAY_BUFFER_ARB, vertexB );
		glInterleavedArrays(GL_T2F_C4F_N3F_V3F,0,0);
		glFinish();
		time = sMillisecs();
		for(int i=0;i<batchs;++i)
		{
			glDrawRangeElements(GL_TRIANGLES,0,ci-1,ci,GL_UNSIGNED_SHORT, 0);
	//		Sleep(1);
		}
		glFinish();
		time = sMillisecs()-time;
		i16s+=time;
		debug::Log<<"16bit indices:"<<time<<std::endl;
		glFinish();
		Flip(1);

		//pass 1
		glFinish();
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB,indices32);
		glBindBuffer(GL_ARRAY_BUFFER_ARB, vertexB );
		glInterleavedArrays(GL_T2F_C4F_N3F_V3F,0,0);
		glFinish();
		time = sMillisecs();
		for(int i=0;i<batchs;++i)
		{
			glDrawRangeElements(GL_TRIANGLES,0,ci-1,ci,GL_UNSIGNED_INT, 0);
	//		Sleep(1);
		}
		glFinish();
		time = sMillisecs()-time;
		i32s+=time;
		debug::Log<<"32bit indices:"<<time<<std::endl;
		glFinish();

		char buff[80];
		sprintf_s(buff,80,"step %d of %d",fc,cntpass);
		AppTitle(buff);
		Flip(1);
	}

	debug::Log<<"16bit (avg):"<<i16s / cntpass<<std::endl;
	debug::Log<<"32bit (avg):"<<i32s / cntpass<<std::endl;

	//glColorMask(1,1,1,1);
	glEnable(GL_DEPTH_TEST);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
	delete[] v;
	delete[] sind;
	delete[] ind;
	debug::Log<<"end scan optimization"<<std::endl;

	Optimizater::indextype = ((i16s>i32s)?32:16);

	std::ofstream fs("optimal.ini");
	fs<<((i16s>i32s)?32:16)<<std::endl;
	fs.close();

}

static int GetPostTnLsz()
{
}

static int GetStripped()
{
}

static int GetOptimized()
{
}

static int GetIndextype()
{
}